import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/resources/kq_theme_colors.dart';
import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';

import '../../utils/kq_multi_click_util.dart';

/// 通用图标正方形按钮
///
/// @author 周卓
///
/// 基于系统的[IconButton]封装， 封装原因：直接用[IconButton]会占用不必要的很多空间
class KqIconButton extends StatelessWidget {
  /// 图标
  final Widget icon;

  /// 点击区域大小，默认40
  final double? size;

  /// 背景颜色，默认透明
  final Color? backgroundColor;

  /// 提示
  final String? tooltip;

  /// 点击回调
  final VoidCallback? onPressed;

  /// 鼠标右键点击回调
  final VoidCallback? onRightPressed;

  /// 是否需要多次点击处理，避免连续点击多次。
  final bool needMultiClickHandle;

  const KqIconButton(
      {Key? key,
      required this.icon,
      this.size,
      this.tooltip,
      this.backgroundColor,
      this.needMultiClickHandle = true,
      this.onPressed,
      this.onRightPressed,})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
      child: SizedBox(
        width: size ?? 32,
        height: size ?? 32,
        child: KqInkWell(
          backgroundColor: backgroundColor ?? KqThemeColors.bgTransparent,
          borderWidth: 0,
          tooltip: tooltip,
          radius: (size ?? 32) / 2,
          onTap: () {
            if (needMultiClickHandle && KqMultiClickUtil.isMultiClick()) {
              return;
            }
            onPressed?.call();
          },
          onSecondaryTap: onRightPressed == null ? null : (details) {
            onRightPressed?.call();
          },
          child: Align(
            child: icon,
          ),
        ),
      ),
    );
  }
}
